import sys

from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QFileDialog, QWidget, QHBoxLayout, QVBoxLayout
from qfluentwidgets import PushButton, TransparentToolButton, FluentIcon, ImageLabel, SubtitleLabel, PrimaryPushButton, TransparentPushButton

from components.base import DragEventBase
from resource import Icon
from config import CONFIG_JSON
from tools import JsonEditor
from pathlib import Path


class IconButton(PrimaryPushButton):
    def __int__(self):
        super().__init__()


class OpenFileButton(PushButton):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.jsonEdit = JsonEditor(CONFIG_JSON)
        self.openFile = self.jsonEdit.get('OpenFile')
        if self.openFile is None:
            self.openFile = ''

    def getExistingDirectory(self):
        file = QFileDialog.getExistingDirectory(self, '选择文件夹', self.openFile)
        self.jsonEdit.setItem('OpenFile', file)
        return file

    def getOpenFileName(self, *args, filter='所有文件(*.*);'):
        file = QFileDialog.getOpenFileName(self, '选择文件', self.openFile, filter)
        if file[0] != '':
            self.jsonEdit.setItem('OpenFile', str(Path(file[0]).resolve().parent))
        return file

    def getOpenFileNames(self, *args, filter='所有文件(*.*);'):
        file = QFileDialog.getOpenFileNames(self, '选择多个文件', self.openFile, filter)
        if len(file[0]) > 0:
            self.jsonEdit.setItem('OpenFile', str(Path(file[0][0]).resolve().parent))
        return file


class CenterButton(QWidget):
    clicked = pyqtSignal()

    def __init__(self, parent=None):
        super().__init__(parent)
        self.hBox = QHBoxLayout(self)
        self.hBox.setContentsMargins(0, 0, 0, 0)
        self.hBox.setSpacing(0)
        self.hBox.setAlignment(Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter)

        self.but = TransparentToolButton()
        self.but.setFixedSize(18, 18)
        self.but.setIcon(FluentIcon.CLOSE)
        self.but.clicked.connect(self.clicked.emit)
        self.hBox.addWidget(self.but, 0, Qt.AlignmentFlag.AlignCenter)


class DragButton(DragEventBase, TransparentPushButton):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setFixedHeight(150)
        self.setAcceptDrops(True)
        self.vBox = QVBoxLayout(self)
        self.vBox.setContentsMargins(0, 0, 0, 0)
        self.vBox.setSpacing(10)
        self.vBox.setAlignment(Qt.AlignmentFlag.AlignCenter)

        self.uploadLabel = ImageLabel(self)
        self.pixmap = QPixmap(
            Icon.UPLOAD.path()
        ).scaled(
            100, 100,
            Qt.AspectRatioMode.KeepAspectRatioByExpanding,
            Qt.TransformationMode.SmoothTransformation
        )

        self.uploadLabel.setPixmap(self.pixmap)
        self.subtitleLabel = SubtitleLabel('将文件拖放到此处或者点此上传', self)

        self.vBox.addWidget(self.uploadLabel, 0, Qt.AlignmentFlag.AlignHCenter)
        self.vBox.addWidget(self.subtitleLabel, 0, Qt.AlignmentFlag.AlignHCenter)
